package com.suyuan.controller.shop.admin;

import com.suyuan.entity.ProPic;
import com.suyuan.entity.ProductInfo;
import com.suyuan.service.ProPicService;
import com.suyuan.service.ProductService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 轮播图管理
 */
@Controller
@RequestMapping(value = "/shop/admin/slidePic")
public class SlidePicController {
    @Resource(name = "proPicService")
    private ProPicService proPicService;
    @Resource(name = "productService")
    private ProductService productService;

    @RequestMapping(value = "/list")
    public String list(ModelMap model){
        List<ProPic> slidePics = productService.getSlidePic();
        model.addAttribute("slidePics",slidePics);
        return "slide_pic_list";
    }

    @RequestMapping(value = "/add")
    public String add(ModelMap model){
        List<ProductInfo> all = productService.findAll();
        model.addAttribute("products",all);
        return "slide_pic_add";
    }

    @RequestMapping(value = "/save")
    public String save(HttpServletRequest request,Integer product){
        try {
            String realPath = request.getServletContext().getRealPath("/WEB-INF/images/");
            if (request instanceof MultipartHttpServletRequest) {
                MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
                // 获取上传的文件集合
                Map<String, MultipartFile> fileMap = multipartHttpServletRequest.getFileMap();
                for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) {
                    // 对文件进处理
                    MultipartFile file = entry.getValue();
                    String uuid = UUID.randomUUID().toString().replace("-","");
                    String extension = FilenameUtils.getExtension(file.getOriginalFilename());
                    String newFileName = uuid + "." + extension;

                    ProPic pic = new ProPic();
                    pic.setUrl("/images/"+newFileName);
                    pic.setType(2);//轮播图类型
                    pic.setProductId(product);
                    proPicService.insert(pic);
                    file.transferTo(new File(realPath+"/"+newFileName));//保存轮播图
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/shop/admin/slidePic/list";
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        int code = proPicService.delete(id);
        return "redirect:/shop/admin/slidePic/list";
    }

}
